package com.qianxx.component.activity.menu.app_dock;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.menu.appdock.AppDock;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AppDockActivity extends BaseActivity {
    @BindView(R.id.left_btn)
    Button left_btn;
    @BindView(R.id.rigth_btn)
    Button rigth_btn;
    @BindView(R.id.app_dock)
    AppDock app_dock;

    private AppDockAdapter adapter;
    private int currentIndex;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_dock);
        ButterKnife.bind(this);

        adapter = new AppDockAdapter(getApplicationContext());
        app_dock.setAdapter(adapter);
        currentIndex = Integer.MAX_VALUE / 2;
        app_dock.setSelection(currentIndex);
    }

    @OnClick({R.id.left_btn,R.id.rigth_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                app_dock.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
                currentIndex--;
                break;

            case R.id.rigth_btn:
                currentIndex++;
                app_dock.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
                break;

            default:
                break;
        }

    }

    class AppDockAdapter extends BaseAdapter {
        private Context context;
        private int count = Integer.MAX_VALUE;
        private List<Integer> imageList = new ArrayList<>();

        public AppDockAdapter(Context context) {
            this.context = context;
            imageList.add(R.drawable.mirror_icon_beauty_black);
            imageList.add(R.drawable.mirror_icon_map_black);
            imageList.add(R.drawable.mirror_icon_news_black);
            imageList.add(R.drawable.mirror_icon_schedule_black);
            imageList.add(R.drawable.mirror_icon_weather_black);
            imageList.add(R.drawable.mirror_icon_wifi_black);

        }

        @Override
        public int getCount() {
            return count;
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder vh = null;
            if (view == null) {
                vh = new ViewHolder();
                view = LayoutInflater.from(context).inflate(
                        R.layout.item_app_dock, null);
                vh.appIcon = (ImageView) view.findViewById(R.id.app_icon);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            vh.appIcon.setImageResource(imageList.get(position
                    % imageList.size()));
            return view;
        }

        class ViewHolder {
            ImageView appIcon;
        }

    }

    @Override
    public String setTitle() {
        return "AppDock";
    }
}
